package org.lanqiao.tjut.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.lanqiao.tjut.bean.UsersEntity;
import org.lanqiao.tjut.service.IUserInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller

public class UserLoginControl {
	// 组合service层，依赖注入
	@Resource
	private IUserInfoService userService;

	@RequestMapping("/login.do")
	public ModelAndView doLogin2(HttpSession session, UsersEntity userE) {
		System.out.println("登录用户名：" + userE.getUsername() + "@" + userE.getUserpsw());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();
		List<UsersEntity> lstUsers = userService.selectUsersInfo4Login(userE);
		if(userE.getUserrole().equals("0")){
			//跳转到管理员界面
			if (lstUsers != null && lstUsers.size() > 0) {
				// 登录成功
				// 登录验证成功，将用户信息进行session缓存
				session.setAttribute("LoginedUserInfo", lstUsers.get(0));
				// 设置逻辑视图名--登录成功
				model.setViewName("logined");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("login");
			}
			
			
		}else if(userE.getUserrole().equals("1")){
			//跳转到操作员界面
			if (lstUsers != null && lstUsers.size() > 0) {
				
				session.setAttribute("LoginedUserInfo", lstUsers.get(0));
				// 设置逻辑视图名--登录成功
				model.setViewName("operatorlogined");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("login");
			}
		}else if(userE.getUserrole().equals("2")){
			//跳转到用户界面
			if (lstUsers != null && lstUsers.size() > 0) {
				session.setAttribute("LoginedUserInfo", lstUsers.get(0));
				// 设置逻辑视图名--登录成功
				model.setViewName("clientlogined");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("login");
			}
		}
		
		return model;
		
	}

	@RequestMapping("/query.do")
	public ModelAndView doQuery(HttpSession session, UsersEntity userE) {
		System.out.println("查询用户名：" + userE.getUsername() + "@" + userE.getUserrole());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();
		List<UsersEntity> lstUser = userService.selectUsersInfoByParas(userE);
		if (lstUser != null && lstUser.size() > 0) {
			// 登录成功

			// 登录验证成功，将用户信息进行session缓存
			session.setAttribute("QueryUserInfo", lstUser);
			// 设置逻辑视图名--登录成功
			model.setViewName("admindoQuery");
		} else {
			// 设置逻辑视图名--登录验证失败
			model.setViewName("userInfoQuery");
		}
		return model;
	}

	@RequestMapping("/adminEdit.do")
	public ModelAndView doEdit(HttpSession session, UsersEntity userE, String uid) {
		System.out.println("查询用户名：" + userE.getUsername() + "@" + userE.getUserrole());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();

		userE = userService.getUserInfoById(uid);

		// 登录验证成功，将用户信息进行session缓存
		session.setAttribute("userE", userE);
		// 设置逻辑视图名--登录成功
		model.setViewName("adminAdd");

		return model;
	}

	@RequestMapping("/adminDelete.do")
	public String doDelete(HttpSession session, UsersEntity userE, String uid) {
		System.out.println("删除用户名：" + userE.getUsername() + "@" + userE.getUserrole());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();

		int i = userService.deleteUserInfo(uid);

		// 设置逻辑视图名--登录成功
		 
		//redirect是不解析成视图
		return "redirect:query.do";
	}

	@RequestMapping("/adminAdd.do")
	public ModelAndView doAdd(HttpSession session, UsersEntity userE) {
		System.out.println("添加用户名：" + userE.getUsername() + "@" + userE.getUserpsw() + "@" + userE.getUserrole());
		// 创建一个modelandview对象
		ModelAndView model = new ModelAndView();

		if (userE.getUserid() == null) {

			int i = userService.saveUserInfo(userE);

			if (i > 0) {
				// 添加成功
				List<UsersEntity> lstUser = userService.selectUsersInfo(userE);
				// 登录验证成功，将用户信息进行session缓存
				session.setAttribute("QueryUserInfo", lstUser);
				// 设置逻辑视图名--登录成功
				model.setViewName("adminManager");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("error");
			}
		} else {
			int j = userService.editUserInfo(userE);
			if (j > 0) {
				// 添加成功
				List<UsersEntity> lstUser = userService.selectUsersInfo(userE);
				// 登录验证成功，将用户信息进行session缓存
				session.setAttribute("QueryUserInfo", lstUser);
				// 设置逻辑视图名--登录成功
				model.setViewName("adminManager");
			} else {
				// 设置逻辑视图名--登录验证失败
				model.setViewName("error");
			}
		}
		return model;
	}
}
